<?php
require_once 'common.php';
/**
 * 应用模型
 * @author xucongbin
 *
 */
class OaJobLogModel extends OpModel
{
    // 开发环境
    const APP_CONFIG_ENV_TEST = 1; // 联调环境
    const APP_CONFIG_ENV_PRODUCTION = 2; // 正式环境
    protected static $envList = array(
        self::APP_CONFIG_ENV_TEST => "联调环境", 
        self::APP_CONFIG_ENV_PRODUCTION => "正式环境"
    );
    
    const JOB_STATUS_SUCCESS = 1;
    const JOB_STATUS_FAILURE = 2;
    protected static $_jobStatus = array(
        self::JOB_STATUS_SUCCESS => '成功', 
        self::JOB_STATUS_FAILURE => '失败'
    );
    const JOB_LOG_TABLE = "app_job_log";
    private $_jobLogTableDb;
    public static function getConstantValueByName ($name, $default = 0)
    {
        return self::_getConstByName($name, '', $default, 'OaJobLogModel');
    }
    public function __construct ($logger = null)
    {
        if (is_null($logger)) {
            $logger = getOaModelLogger();
        }
        parent::__construct($logger);
        $dbConfig = Oa_Util_Config::getSingleDbConfig(OA_WEB_DB_CLUSTER_ID);
        $db = OpDbModel::getInstance($dbConfig, $logger);
        $this->_jobLogTableDb = new OpTableModel($db, self::JOB_LOG_TABLE, $logger);
    }
    /**
     * 获取工作项状态 
     * @param int $status 
     * 
     * @return array|string $status不为null，返回状态名称；其它返回全部动作
     */
    public function getJobStatus ($status = null)
    {
        if ($status) {
            return self::$_jobStatus[$status];
        }
        return self::$_jobStatus;
    }
    /**
     * 获取记录列表
     * @param array $where array(environment=> 环境
     * app_id=> 产品ID
     * order_id=> 订单号
     * job_name=>工作项名称)
     * @param int $offset
     * @param int $count 
     * @param string $sort
     * @param array $otherWhere 附加条件
     * 
     * @return mixed
     */
    public function getJobLog ($where, $offset = 0, $count = null, $sort = null, $otherWhere=array())
    {        
        $sql = "SELECT * FROM " . self::JOB_LOG_TABLE . getSqlCond($where, $offset, $count, $sort, 
            $otherWhere);
        $result = $this->_jobLogTableDb->query($sql);
        if ($result === false) {
            $this->_logger->log("query failed");
            return false;
        }
        return $result;
    }
    /**
     * 获取记录条数
     * @param array $where array(environment=> 环境
     * app_id=> 产品ID
     * order_id=> 订单号
     * job_name=>工作项名称)
     * @param array 附加条件 $otherWhere
     * 
     * @return array|boolean
     */
    public function getJobLogCount ($where, $otherWhere=array())
    {
        $sql = "SELECT COUNT(*) FROM " . self::JOB_LOG_TABLE . getSqlCond($where, 0, 0, null, 
            $otherWhere);
        $result = $this->_jobLogTableDb->query($sql, PDO::FETCH_NUM);
        if ($result === false) {
            $this->_logger->log("query failed");
            return false;
        }
        return (int) $result[0][0];
    }
    /**
     * 添加工作项日志
     * @param string $orderId 订单号
     * @param string $jobName 工作项名称
     * @param string $creator 创建者
     * @param array $row array(`app_id`=>应用ID, `status`=>任务状态，1成功，2失败, `message`=>信息，
     * `download_link`=>下载链接, `create_time`=>添加时间)
     * @param int $env 环境
     * 
     * @return boolean
     */
    public function createJobLog ($orderId, $jobName, $creator, $row = array(), $env = self::APP_CONFIG_ENV_TEST)
    {
        $row = array_merge(array(
            'environment' => $env,
            'order_id' => $orderId, 
            'job_name' => $jobName, 
            'creator' => $creator
        ), $row);
        
        if (! isset($row['create_time'])) {
            $row['create_time'] = date('Y-m-d H:i:s');
        }
        return $this->_jobLogTableDb->insert($row);
    }
    /**
     * 删除
     * @param array $where array(environment=> 环境
     * app_id=> 产品ID
     * order_id=> 订单号
     * job_name=>工作项名称) 可选或多个
     * @return boolean
     */
    public function deleteJobLog($where)
    {
        if(!$where){
            return false;
        }
        return $this->_jobLogTableDb->delete($where);
    }
}